/**
 * ***************************************
 * @file ymodemexample.c
 * @author : {Lek} (emimand1010@gmail.com)
 * @brief : 
 * @version 0.1
 * @date : 2024-11-23
 * @copyright Copyright (c) 2024
 * 
 * ***************************************
 */
const char *namefile = "demo.txt";
int number_to_ascii_string(int num, char *result)
{
    // 使用 sprintf 将数字转换为字符串，并返回字符串的长度
    int length = sprintf(result, "%d", num);
    return length;
}

char random_numbers[2500] = {
    10, 45, 97, 12, 68, 31, 73, 7, 19, 76,
    11, 21, 89, 62, 86, 100, 25, 39, 19, 27,
    5, 39, 42, 69, 13, 72, 35, 70, 11, 94,
    78, 87, 48, 57, 17, 3, 63, 76, 75, 14,
    8, 45, 28, 66, 10, 39, 92, 69, 6, 38,
    7, 23, 34, 65, 52, 15, 43, 43, 46, 26,
    78, 96, 38, 87, 73, 76, 13, 33, 22, 54,
    86, 81, 14, 31, 72, 9, 6, 87, 71, 69,
    76, 22, 23, 97, 68, 5, 57, 4, 90, 46,
    71, 66, 27, 34, 44, 74, 51, 81, 14, 15,
    86, 62, 32, 82, 63, 8, 61, 49, 59, 46,
    46, 67, 22, 52, 67, 95, 29, 73, 9, 28,
    73, 97, 48, 66, 54, 4, 16, 25, 43, 16,
    66, 15, 23, 19, 92, 32, 41, 24, 27, 2,
    23, 0, 35, 9, 51, 24, 40, 72, 13, 59,
    42, 92, 72, 24, 69, 56, 95, 8, 65, 34,
    50, 18, 28, 34, 12, 54, 92, 78, 4, 63,
    80, 28, 73, 37, 78, 91, 100, 93, 41, 41,
    40, 53, 21, 17, 27, 72, 5, 33, 76, 9,
    24, 3, 6, 96, 76, 65, 60, 68, 71, 1,
    42, 66, 3, 74, 99, 9, 22, 43, 45, 0,
    87, 74, 39, 89, 8, 6, 94, 100, 30, 74,
    52, 19, 91, 70, 96, 7, 24, 75, 53, 27,
    92, 16, 15, 100, 94, 80, 20, 38, 88, 32,
    24, 87, 26, 77, 91, 80, 82, 8, 75, 79,
    51, 75, 93, 7, 25, 17, 72, 91, 16, 3,
    93, 71, 58, 80, 7, 76, 42, 43, 21, 86,
    8, 3, 29, 50, 75, 53, 91, 83, 81, 96,
    2, 66, 43, 57, 83, 38, 96, 98, 11, 72,
    71, 6, 85, 79, 38, 22, 7, 95, 18, 8,
    3, 100, 48, 99, 88, 89, 3, 13, 37, 87,
    5, 18, 32, 91, 35, 12, 6, 6, 70, 86,
    69, 100, 81, 53, 20, 9, 97, 66, 88, 76,
    32, 14, 87, 90, 52, 5, 9, 12, 20, 54,
    59, 3, 84, 10, 3, 46, 35, 75, 71, 35,
    40, 66, 36, 80, 75, 15, 32, 31, 77, 37,
    100, 79, 13, 9, 96, 76, 72, 35, 14, 16,
    0, 18, 73, 4, 32, 52, 97, 93, 89, 23,
    78, 37, 63, 59, 94, 32, 49, 69, 91, 68,
    82, 13, 57, 21, 61, 14, 80, 41, 30, 34,
    99, 37, 19, 86, 94, 13, 25, 75, 34, 62,
    70, 92, 20, 57, 25, 74, 62, 28, 47, 68,
    16, 4, 49, 20, 61, 36, 8, 78, 52, 77,
    48, 29, 2, 12, 66, 52, 87, 18, 88, 96,
    91, 43, 20, 40, 9, 99, 39, 80, 46, 61,
    53, 63, 61, 38, 42, 3, 46, 96, 77, 82,
    88, 93, 30, 29, 5, 71, 20, 47, 59, 98,
    33, 8, 67, 94, 57, 44, 9, 72, 31, 52,
    0, 65, 30, 27, 15, 76, 60, 80, 91, 97,
    23, 46, 83, 47, 40, 76, 22, 44, 66, 93,
    43, 20, 47, 77, 9, 39, 36, 45, 67, 54,
    24, 47, 21, 96, 90, 95, 90, 82, 14, 19,
    70, 45, 40, 3, 100, 100, 48, 16, 70, 9,
    49, 43, 67, 19, 82, 41, 23, 74, 51, 21,
    85, 13, 40, 66, 52, 2, 96, 53, 27, 79,
    40, 75, 79, 40, 41, 2, 35, 42, 92, 2,
    94, 36, 96, 18, 74, 42, 54, 37, 91, 17,
    75, 17, 25, 72, 10, 40, 5, 27, 60, 49,
    76, 20, 43, 89, 30, 74, 32, 64, 94, 33,
    1, 100, 23, 33, 95, 17, 62, 81, 27, 80,
    47, 47, 6, 48, 71, 76, 58, 82, 92, 16,
    44, 37, 88, 17, 89, 31, 67, 6, 24, 36,
    10, 8, 19, 69, 85, 82, 35, 26, 1, 28,
    6, 75, 9, 52, 30, 75, 72, 23, 60, 79,
    11, 87, 22, 39, 16, 33, 36, 16, 100, 73,
    99, 80, 59, 76, 86, 4, 16, 34, 33, 52,
    27, 22, 59, 85, 98, 64, 69, 12, 53, 54,
    70, 95, 71, 38, 20, 65, 95, 71, 60, 56,
    86, 80, 81, 20, 56, 88, 16, 86, 55, 69,
    79, 62, 50, 9, 81, 55, 33, 37, 12, 69,
    8, 77, 7, 100, 3, 73, 15, 34, 2, 72,
    80, 22, 18, 39, 90, 100, 13, 22, 70, 48,
    35, 9, 64, 76, 62, 13, 15, 35, 81, 44,
    15, 86, 7, 9, 23, 3, 52, 57, 94, 21,
    29, 55, 39, 36, 95, 40, 93, 37, 22, 48,
    7, 24, 41, 64, 72, 73, 7, 40, 67, 24,
    13, 42, 37, 52, 96, 46, 92, 32, 5, 10,
    5, 92, 6, 75, 99, 8, 99, 58, 11, 32,
    86, 7, 13, 9, 9, 85, 62, 17, 76, 64,
    68, 25, 35, 81, 33, 56, 62, 80, 43, 99,
    4, 57, 19, 1, 0, 62, 53, 63, 81, 0,
    28, 86, 19, 4, 38, 19, 37, 68, 18, 53,
    85, 61, 31, 28, 72, 76, 59, 39, 4, 53,
    24, 22, 93, 43, 73, 88, 5, 34, 76, 23,
    95, 31, 4, 84, 91, 38, 39, 29, 54, 6,
    66, 0, 76, 96, 32, 37, 17, 6, 38, 100,
    43, 13, 33, 6, 14, 71, 36, 29, 96, 13,
    31, 44, 76, 51, 92, 89, 90, 55, 19, 90,
    31, 11, 16, 51, 4, 27, 19, 49, 64, 5,
    21, 12, 88, 32, 88, 26, 97, 10, 91, 79,
    9, 97, 39, 23, 29, 67, 2, 88, 52, 45,
    33, 53, 36, 56, 60, 25, 100, 34, 65, 2,
    21, 80, 92, 47, 90, 38, 26, 58, 26, 7,
    5, 84, 9, 0, 51, 60, 80, 13, 94, 33,
    14, 80, 100, 78, 77, 54, 65, 99, 99, 26,
    52, 14, 64, 35, 63, 55, 78, 57, 8, 18,
    72, 97, 17, 69, 74, 86, 16, 1, 41, 53,
    13, 70, 91, 87, 14, 6, 86, 23, 32, 65,
    54, 100, 30, 69, 93, 99, 64, 12, 53, 49,
    64, 25, 49, 92, 74, 93, 23, 17, 92, 9,
    23, 46, 30, 16, 88, 25, 40, 48, 26, 56,
    47, 35, 63, 4, 1, 15, 15, 41, 54, 72,
    88, 17, 95, 80, 75, 64, 94, 19, 57, 74,
    25, 24, 25, 14, 43, 76, 94, 65, 30, 95,
    67, 6, 31, 18, 30, 92, 38, 30, 36, 9,
    89, 89, 55, 48, 59, 97, 43, 44, 83, 14,
    73, 55, 36, 0, 58, 82, 85, 2, 26, 21,
    14, 21, 72, 24, 24, 10, 82, 21, 47, 88,
    5, 41, 63, 78, 16, 18, 67, 92, 7, 49,
    9, 99, 0, 42, 34, 31, 82, 69, 95, 9,
    83, 10, 66, 1, 3, 88, 81, 56, 87, 86,
    60, 30, 29, 90, 64, 67, 18, 52, 54, 34,
    40, 73, 63, 64, 75, 85, 96, 93, 65, 45,
    81, 94, 60, 85, 90, 23, 35, 72, 55, 71,
    28, 45, 4, 49, 94, 84, 71, 30, 85, 53,
    70, 77, 85, 32, 96, 89, 43, 0, 33, 94,
    86, 21, 79, 52, 85, 74, 30, 47, 91, 40,
    78, 38, 47, 88, 10, 100, 8, 9, 34, 65,
    49, 80, 94, 96, 35, 24, 32, 98, 76, 94,
    44, 65, 95, 75, 55, 97, 89, 35, 13, 29,
    39, 34, 17, 71, 93, 23, 53, 68, 100, 62,
    38, 41, 79, 14, 89, 47, 94, 21, 21, 43,
    0, 95, 3, 31, 7, 2, 4, 80, 56, 7,
    86, 5, 12, 29, 85, 56, 1, 99, 6, 22,
    6, 83, 12, 80, 83, 34, 72, 95, 46, 84,
    17, 51, 70, 76, 47, 99, 12, 38, 12, 46,
    99, 47, 8, 61, 1, 31, 64, 8, 10, 18,
    80, 63, 74, 51, 22, 72, 68, 66, 77, 50,
    9, 25, 94, 80, 18, 54, 5, 9, 90, 76,
    66, 84, 61, 0, 67, 4, 75, 89, 9, 12,
    81, 69, 35, 49, 25, 49, 72, 63, 81, 71,
    56, 52, 2, 72, 51, 34, 8, 33, 40, 7,
    70, 17, 19, 76, 77, 30, 89, 89, 72, 48,
    50, 41, 11, 8, 75, 93, 23, 65, 15, 35,
    23, 5, 29, 84, 11, 13, 75, 44, 4, 1,
    61, 73, 56, 44, 29, 34, 19, 18, 80, 43,
    97, 6, 47, 10, 10, 15, 60, 51, 19, 25,
    40, 40, 38, 73, 59, 49, 42, 88, 50, 72,
    44, 6, 8, 36, 47, 23, 20, 42, 17, 84,
    23, 83, 21, 20, 42, 65, 100, 23, 94, 83,
    75, 2, 90, 38, 57, 26, 71, 97, 9, 91,
    10, 80, 43, 29, 16, 0, 23, 4, 82, 30,
    24, 97, 64, 92, 38, 90, 83, 94, 16, 45,
    73, 100, 70, 99, 69, 84, 98, 32, 29, 38,
    94, 23, 48, 35, 2, 99, 21, 98, 97, 9,
    66, 57, 3, 47, 33, 50, 88, 0, 73, 62,
    91, 96, 45, 40, 100, 72, 64, 9, 97, 62,
    92, 38, 29, 82, 84, 61, 42, 17, 34, 77,
    7, 66, 43, 87, 86, 89, 71, 15, 12, 85,
    47, 49, 78, 12, 15, 11, 9, 100, 0, 25,
    29, 41, 98, 35, 97, 95, 23, 61, 51, 91,
    1, 17, 41, 70, 34, 74, 82, 92, 18, 48,
    88, 16, 21, 58, 86, 37, 74, 14, 47, 67,
    12, 59, 62, 59, 26, 59, 59, 56, 72, 70,
    100, 24, 81, 29, 71, 13, 14, 87, 60, 56,
    76, 48, 26, 87, 82, 61, 26, 10, 84, 51,
    52, 75, 72, 10, 20, 88, 31, 54, 84, 17,
    54, 39, 45, 51, 50, 26, 79, 16, 49, 85,
    80, 66, 84, 59, 22, 95, 31, 71, 17, 85,
    10, 94, 12, 26, 32, 63, 50, 76, 30, 34,
    72, 7, 65, 39, 76, 36, 83, 54, 67, 27,
    76, 15, 57, 29, 77, 58, 5, 27, 21, 3,
    14, 64, 32, 19, 68, 45, 47, 9, 62, 3,
    0, 25, 28, 58, 36, 51, 65, 56, 76, 1,
    71, 48, 68, 23, 61, 97, 71, 53, 80, 43,
    33, 32, 26, 40, 1, 91, 7, 0, 76, 90,
    83, 9, 19, 77, 39, 60, 93, 66, 2, 39,
    66, 86, 63, 37, 51, 38, 60, 67, 38, 77,
    37, 7, 37, 76, 64, 3, 13, 50, 87, 78,
    15, 57, 71, 51, 56, 100, 25, 16, 9, 38,
    40, 86, 32, 99, 26, 4, 29, 53, 64, 100,
    25, 60, 84, 4, 16, 19, 73, 55, 24, 48,
    62, 7, 30, 87, 87, 40, 100, 72, 81, 62,
    5, 34, 15, 27, 98, 89, 27, 96, 36, 47,
    0, 49, 22, 2, 49, 64, 80, 31, 5, 26,
    34, 75, 52, 49, 22, 75, 19, 15, 87, 3,
    15, 48, 12, 84, 7, 99, 49, 14, 59, 39,
    42, 51, 25, 43, 96, 18, 57, 85, 48, 18,
    84, 15, 92, 1, 2, 67, 65, 16, 23, 90,
    89, 47, 1, 54, 17, 26, 67, 52, 49, 89,
    99, 22, 49, 41, 75, 99, 34, 80, 3, 23,
    88, 18, 93, 49, 21, 0, 46, 74, 77, 84,
    23, 36, 26, 45, 18, 6, 90, 27, 67, 66,
    6, 83, 50, 78, 25, 56, 18, 89, 98, 51,
    41, 29, 98, 69, 58, 17, 31, 23, 67, 17,
    55, 37, 96, 39, 99, 77, 53, 5, 37, 59,
    40, 83, 38, 86, 3, 97, 79, 17, 72, 95,
    45, 88, 97, 20, 60, 71, 86, 66, 15, 60,
    38, 60, 69, 33, 75, 62, 85, 55, 81, 84,
    17, 59, 98, 98, 54, 13, 2, 29, 52, 72,
    90, 65, 34, 20, 30, 6, 58, 96, 6, 50,
    87, 26, 46, 16, 57, 11, 79, 51, 46, 35,
    78, 9, 7, 28, 62, 35, 10, 59, 90, 59,
    39, 63, 39, 46, 75, 14, 94, 80, 48, 93,
    28, 0, 89, 100, 80, 87, 5, 65, 98, 50,
    64, 36, 54, 30, 78, 50, 38, 31, 30, 63,
    71, 13, 31, 26, 1, 60, 33, 1, 55, 29,
    99, 17, 13, 15, 38, 86, 89, 50, 26, 79,
    40, 84, 66, 20, 13, 17, 37, 40, 27, 13,
    75, 92, 48, 68, 23, 8, 72, 86, 59, 73,
    48, 70, 78, 45, 80, 40, 68, 87, 5, 20,
    42, 94, 0, 8, 4, 61, 49, 79, 72, 17,
    70, 53, 69, 100, 77, 47, 38, 23, 15, 18,
    9, 81, 60, 31, 32, 20, 84, 57, 62, 5,
    86, 74, 77, 35, 75, 18, 74, 77, 6, 49,
    50, 41, 20, 67, 46, 6, 32, 88, 57, 36,
    11, 58, 67, 93, 28, 35, 73, 13, 0, 25,
    56, 71, 93, 54, 70, 59, 9, 95, 82, 92,
    74, 12, 56, 21, 30, 66, 18, 39, 46, 27,
    80, 29, 32, 81, 59, 29, 52, 90, 36, 20,
    8, 51, 18, 53, 63, 70, 24, 52, 96, 1,
    2, 74, 50, 22, 10, 86, 46, 27, 96, 67,
    37, 16, 10, 0, 17, 36, 8, 36, 99, 69,
    94, 49, 19, 84, 0, 62, 77, 93, 42, 60,
    48, 3, 18, 27, 98, 95, 50, 20, 55, 39,
    69, 74, 22, 85, 36, 8, 55, 16, 86, 13,
    43, 24, 67, 19, 93, 72, 83, 11, 44, 49,
    98, 49, 84, 85, 55, 53, 78, 28, 94, 36,
    74, 85, 77, 7, 79, 32, 55, 37, 94, 3,
    56, 17, 43, 80, 100, 66, 86, 12, 10, 67,
    53, 49, 100, 1, 94, 64, 51, 21, 70, 22,
    96, 79, 53, 2, 68, 30, 6, 35, 89, 23,
    79, 45, 84, 16, 13, 21, 83, 91, 51, 96,
    54, 44, 1, 77, 46, 94, 69, 6, 30, 32,
    36, 87, 10, 35, 77, 37, 54, 27, 30, 57,
    15, 17, 94, 14, 14, 96, 75, 39, 96, 80,
    36, 92, 76, 21, 68, 18, 49, 97, 27, 71,
    71, 71, 60, 95, 8, 83, 95, 59, 24, 18,
    21, 10, 80, 64, 68, 13, 14, 82, 31, 93,
    81, 30, 98, 36, 36, 70, 36, 10, 82, 88,
    14, 87, 98, 66, 75, 28, 14, 4, 27, 1,
    89, 92, 75, 98, 97, 97, 18, 80, 80, 91,
    25, 44, 46, 3, 80, 100, 27, 9, 64, 50,
    10, 72, 34, 49, 34, 99, 89, 48, 6, 79,
    37, 42, 31, 80, 12, 62, 14, 50, 69, 35,
    40, 47, 23, 81, 67, 87, 97, 87, 93, 34,
    10, 74, 34, 56, 23, 49, 76, 30, 67, 58,
    85, 89, 65, 3, 18, 51, 67, 36, 28, 16,
    93, 89, 7, 96, 63, 79, 13, 83, 67, 67,
    42, 0, 88, 96, 85, 1, 100, 96, 89, 86,
    63, 60, 63, 51, 87, 42, 88, 54, 39, 60,
    25, 93, 9, 8, 94, 1, 13, 86, 90, 22,
    21, 23, 54, 46, 85, 44, 55, 85, 85, 42,
    44, 75, 98, 88, 21, 31, 8, 70, 98, 33,
    71, 8, 86, 65, 97, 38, 42, 16, 60, 3,
    27, 34, 50, 82, 89, 2, 16, 65, 20, 28,
    11, 96, 95, 40, 13, 63, 47, 79, 9, 50,
    5, 22, 92, 56, 84, 28, 94, 76, 85, 97,
    53, 13, 89, 24, 57, 26, 35, 63, 42, 55,
    0, 63, 29, 96, 74, 3, 40, 7, 43, 21};

/*起始帧*/
static uint32_t current_index = 0;
static enum rym_code ymodem_on_begin(RYM_t *pxRYM_Ag, uint8_t *buf, uint32_t len)
{
    printf("len = %ld\r\n", len);
    current_index = 0;
    uint16_t len1 = 0;
    uint16_t len2 = strlen(namefile);
    uint8_t buf2[20];
    strncpy((char *)buf, namefile, len);
    len1 += len2;
    // len1++;
    buf[len1] = 0x00;
    len1++;
    printf("len1 = %d sizeof(random_numbers) = %d\r\n", len1, sizeof(random_numbers));
    int len3 = number_to_ascii_string(sizeof(random_numbers), (char *)buf2);
    printf("len3 = %d buf2= %s\r\n", len3, buf2);
    // memset(buf,);
    strncpy((char *)&buf[len1], (char *)buf2, len3);
    len1 += len3;
    // len1++;
    buf[len1] = 0x20;
    len1++;
    printf("len = %d\r\n", len1);
    memset(&buf[len1], 0x00, len - len1 - 1);
    return RYM_CODE_ACK;
}
/*数据帧*/

static enum rym_code ymodem_on_data(RYM_t *pxRYM_Ag, uint8_t *buf, uint32_t len)
{

    // 检查是否还有更多的数据需要复制
    if (current_index < sizeof(random_numbers))
    {
        // 计算本次迭代需要复制的字节数
        uint32_t bytes_to_copy = (sizeof(random_numbers) - current_index >= 1024) ? 1024 : (sizeof(random_numbers) - current_index);

        // 将 random_numbers 中的数据复制到 buf 中
        memcpy(buf, &random_numbers[current_index], bytes_to_copy);

        // 如果复制的数据少于 1024 字节，则使用 0x1f 填充剩余部分
        if (bytes_to_copy < 1024)
        {
            pxRYM_Ag->stage = RYM_STAGE_FINISHING;
            memset(buf + bytes_to_copy, 0x1f, 1024 - bytes_to_copy);
        }

        // 更新当前索引，指向下一个数据段
        current_index += bytes_to_copy;
    }
    else
    {
        // 没有更多数据可发送，返回错误代码或其他合适的代码
        return RYM_ERR_CODE;
    }
    printf("current_index = %ld ,sizeof = %d\r\n", current_index, sizeof(random_numbers));
    return RYM_CODE_ACK;
}

static enum rym_code ymodem_on_fin(RYM_t *pxRYM_Ag, uint8_t *buf, uint32_t len)
{
    printf("ymodem_on_fin\r\n");
    memset(buf, 0x00, len);
    return RYM_CODE_ACK;
}

static enum rym_code ymodem_on_begin1(RYM_t *pxRYM_Ag, uint8_t *buf, uint32_t len)
{

    return RYM_CODE_ACK;
}


static enum rym_code ymodem_on_data1(RYM_t *pxRYM_Ag, uint8_t *buf, uint32_t len)
{

  
    return RYM_CODE_ACK;
}

RYM_t xRYM_Ag;

void ymodem_receive()
{

    RYM_ReadFile(&xRYM_Ag, ymodem_on_begin1, ymodem_on_data1, NULL);
    vTaskDelay(1000);
    RYM_SendFile(&xRYM_Ag, ymodem_on_begin, ymodem_on_data, ymodem_on_fin);
}